package com.icode.web.validator;

import com.icode.core.dto.manager.SellManagerDTO;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-8-16
 * Time: 下午12:08
 * To change this template use File | Settings | File Templates.
 */
@Component
public class SellManagerValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return SellManagerDTO.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        SellManagerDTO sellManagerDTO = (SellManagerDTO) target;
        String gold = sellManagerDTO.getGold();
        String busy = sellManagerDTO.getBusy();
        String slack = sellManagerDTO.getSlack();
        String common = sellManagerDTO.getCommon();
        if (!Pattern.matches("[\\d]*", gold)) {
            errors.rejectValue("gold", null, "只能输入数字!");
        }
        if (!Pattern.matches("[\\d]*", busy)) {
            errors.rejectValue("busy", null, "只能输入数字!");
        }
        if (!Pattern.matches("[\\d]*", slack)) {
            errors.rejectValue("slack", null, "只能输入数字!");
        }
        if (!Pattern.matches("[\\d]*", common)) {
            errors.rejectValue("common", null, "只能输入数字!");
        }
    }
}
